package com.ruoyi.book.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.book.domain.Book;
import com.ruoyi.book.service.IBookService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 书本信息Controller
 *
 * @author ruoyi
 * @date 2024-04-02
 */

@RestController
@RequestMapping("/book/info")
public class BookController extends BaseController {
    @Autowired
    private IBookService bookService;

    /**
     * 查询书本信息列表
     */
    @PreAuthorize("@ss.hasPermi('book:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(Book book) {
        startPage();
        List<Book> list = bookService.selectBookList(book);
        return getDataTable(list);
    }

    /**
     * 导出书本信息列表
     */
    @PreAuthorize("@ss.hasPermi('book:info:export')")
    @Log(title = "书本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Book book) {
        List<Book> list = bookService.selectBookList(book);
        ExcelUtil<Book> util = new ExcelUtil<Book>(Book.class);
        util.exportExcel(response, list, "书本信息数据");
    }

    /**
     * 获取书本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('book:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bookService.selectBookById(id));
    }

    /**
     * 新增书本信息
     */
    @PreAuthorize("@ss.hasPermi('book:info:add')")
    @Log(title = "书本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Book book) {
        if (bookService.checkBookNumUnique(book)) {
            return AjaxResult.warn("书本编号已存在");
        } else if (!StringUtils.isNotEmpty(book.getName())) {
            return AjaxResult.warn("书本名称不能为空");
        } else if (!StringUtils.isNotEmpty(book.getAuthor())) {
            return AjaxResult.warn("书本作者不能为空");
        } else if (!StringUtils.isNotEmpty(book.getPublisher())) {
            return AjaxResult.warn("书本出版社不能为空");
        } else if (!StringUtils.isNotEmpty(book.getPublicationDate().toString())) {
            return AjaxResult.warn("书本出版日期不能为空");
        } else if (!StringUtils.isNotEmpty(book.getType())){
            return AjaxResult.warn("书本类型不能为空");
        }
        book.setCreateBy(getUsername());
        return toAjax(bookService.insertBook(book));
    }

    /**
     * 修改书本信息
     */
    @PreAuthorize("@ss.hasPermi('book:info:edit')")
    @Log(title = "书本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Book book) {
        book.setUpdateBy(getUsername());
        return toAjax(bookService.updateBook(book));
    }

    /**
     * 删除书本信息
     */
    @PreAuthorize("@ss.hasPermi('book:info:remove')")
    @Log(title = "书本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bookService.deleteBookByIds(ids));
    }
}
